package cn.wy.org.service.impl;

import cn.wy.basic.mapper.BaseMapper;
import cn.wy.basic.service.BaseService;
import cn.wy.basic.service.impl.BaseServiceImpl;
import cn.wy.basic.util.PageList;
import cn.wy.org.domain.Department;
import cn.wy.org.mapper.DepartmentMapper;
import cn.wy.org.query.DepartmentQuery;
import cn.wy.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> deptTree() {
        List<Department> departments = departmentMapper.findAll();
        Map<Long, Department> map = new HashMap<>();
        for (Department department : departments) {
            map.put(department.getId(),department);
        }

        List<Department> deptTree = new ArrayList<>();
        for (Department department : departments) {
            if (department.getParent_id()==null) {
                deptTree.add(department);
            }else {
                    Department parentDepartment = map.get(department.getParent_id());
                    parentDepartment.getChildren().add(department);
            }
        }
        return deptTree;
    }

}
